//
//  ZZXNiceScrollView.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "ZZXNiceScrollView.h"

@interface ZZXNiceScrollView ()<UIScrollViewDelegate>


@property (nonatomic, strong)NSMutableArray *buttonArr;
@property (nonatomic, strong) UIView *sliderView;
@property (nonatomic, strong) UIButton *button;

@end

@implementation ZZXNiceScrollView

//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrollView];
    }
    return  self;
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:247 / 255.0 blue:237 / 255.0 alpha:1];
    [self addSubview:self.scrollView];
    //隐藏水平滑动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //设置滑动条
    self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(25, 45, 40, 2)];
    self.sliderView.backgroundColor = [UIColor colorWithRed:89 / 255.0 green:186 / 255.0 blue:199 / 255.0 alpha:1];
    self.sliderView.layer.masksToBounds = YES;
    self.sliderView.layer.cornerRadius = 8;
    [self.scrollView addSubview:self.sliderView];
    self.sliderView.tag = 3200;
    
    self.buttonArr = [@[@"最新", @"热门", @"亚洲", @"欧洲", @"美洲", @"非洲", @"大洋洲"]mutableCopy];
    //遍历buttonArr
    for (NSInteger i = 0; i < 7; i++) {
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button.tag = 3000 + i;
        [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //当没有点击时  默认第一个为点击状态
        if (i == 0) {
            [self.button setTitleColor:[UIColor colorWithRed:0.71 green:0.78 blue:0.73 alpha:1]forState:UIControlStateNormal];
            self.button.titleLabel.font = [UIFont systemFontOfSize:17];
        }
        //按钮触发事件
        [self.button addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:self.button];
    }
}

    - (void)layoutSubviews
    {
        [super layoutSubviews];
//        //夜间模式
//        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNight) name:@"ISNIGHT" object:user];
//        
//        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
//            self.scrollView.backgroundColor = [UIColor blackColor];
//        
//        }else
//        {
//            self.scrollView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//        }
        
        self.scrollView.frame = CGRectMake(0, 0, VWIDTH, VHEIGHT);
        //设置偏移量
        
        self.scrollView.contentSize = CGSizeMake(VWIDTH / 5 * 7 + 70, 0);
        //遍历给button frame值
        for (NSInteger i = 0; i < 7; i++) {
            ((UIButton *)[self viewWithTag:3000 + i]).frame = CGRectMake(i * VWIDTH / 5 + 10, 15, 70, 25);
            [((UIButton *)[self viewWithTag:3000 + i]) setTitle:self.buttonArr[i] forState:UIControlStateNormal];
            
            
        }
        
    }
    
    
//    - (void)changeNight
//    {
//        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
//            self.scrollView.backgroundColor = [UIColor blackColor];
//        }
//        else
//        {
//            self.scrollView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//            
//        }
//    }
    
- (void)changePage:(UIButton *)btn
    {
        //让代理人实现协议方法 代理人为显示的controller
        [self.delegate ChangeFontSizeAndColor:btn];
        
        
        for (NSInteger i = 0; i < 7; i++) {
            
            if (btn.tag == 3000 + i) {
                [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
                    self.sliderView.frame = CGRectMake(i * VWIDTH / 5 + 10, 15, 70, 25);
                } completion:^(BOOL finished) {
                    // NSLog(@"meatButton点击~");
                }];
            }
            
            
        }
   
    
    }


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
